home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 41 / Amiga Format CD41 (1999-06)(Future Publishing)(GB)[!][issue 1999-07].iso / -seriously_amiga- / misc / vinced / include / examples / readconsolewindow.c next >
C/C++ Source or Header  |  1999-04-19  |  9KB  |  241 lines

  1. /*****************************************************************
  2.  ** GetWindowPointer                                            **
  3.  ** Get the intuition window of a console handler               **
  4.  **-------------------------------------------------------------**
  5.  ** This program demonstrates how to read the intuition window  **
  6.  ** of a console handler in a way that doesn't break            **
  7.  ** iconification.                                              **
  8.  **                                                             **
  9.  ** Version 1.00 22 Aug 1998            © 1998 THOR-Software    **
  10.  ** Thomas Richter                                              **
  11.  *****************************************************************/
  12.  
  13. #include <exec/types.h>
  14. #include <exec/memory.h>
  15. #include <dos/dos.h>
  16. #include <dos/dosextens.h>
  17. #include <intuition/intuition.h>
  18.  
  19. #include <vnc/packets.h>
  20.  
  21. #include <proto/exec.h>
  22. #include <proto/dos.h>
  23. #include <proto/graphics.h>
  24.  
  25. struct GfxBase *GfxBase;
  26.  
  27.  
  28. #define CON_ID  (('C'<<24L)|('O'<<16L)|('N'<<8L))
  29. #define RAW_ID  (('R'<<24L)|('A'<<16L)|('W'<<8L))
  30.  
  31. /* Prototypes */
  32. struct Window *FindConsoleWindow(BPTR file);
  33. void ReleaseConsoleWindow(BPTR file);
  34.  
  35. int main(int argc,char **argv)
  36. {
  37. struct Window *win;
  38. struct RastPort *rp;
  39. long xmin,ymin,xmax,ymax;
  40. long deltax,deltay;
  41. long x1,y1,x2,y2;
  42.  
  43. /* first, open the required libraries */
  44.  
  45.         if (GfxBase=(struct GfxBase *)OpenLibrary("graphics.library",37L)) {
  46.  
  47. /* read the window pointer */
  48.  
  49.                 if (win=FindConsoleWindow(Output())) {
  50.  
  51. /* Draw some graphics in this window.
  52.    This requires some slight modifications if the window is a
  53.    GZZ window, but so what. It's for demonstration anyways. */
  54.  
  55.                         xmin = win->BorderLeft;
  56.                         ymin = win->BorderTop;
  57.                         xmax = win->Width-win->BorderRight;
  58.                         ymax = win->Height-win->BorderBottom;
  59.                         deltax = (xmax-xmin+1)/16;
  60.                         deltay = (ymax-ymin+1)/16;
  61.  
  62.                         x1 = xmin;
  63.                         y1 = ymin;
  64.                         x2 = xmin;
  65.                         y2 = ymax;
  66.  
  67.                         rp =win->RPort;
  68.  
  69.                         SetAPen(rp,2L);
  70.  
  71.                         while (y1<ymax && x2<xmax) {
  72.                                 Move(rp,x1,y1);
  73.                                 Draw(rp,x2,y2);
  74.                                 y1 += deltay;
  75.                                 x2 += deltax;
  76.                         }
  77.  
  78.                         x1 = xmin;
  79.                         y1 = ymax;
  80.                         x2 = xmax;
  81.                         y2 = ymax;
  82.  
  83.                         while (x1<=xmax && y2>=ymin) {
  84.                                 Move(rp,x1,y1);
  85.                                 Draw(rp,x2,y2);
  86.                                 x1 += deltax;
  87.                                 y2 -= deltay;
  88.                         }
  89.  
  90.                         x1 = xmax;
  91.                         y1 = ymax;
  92.                         x2 = xmax;
  93.                         y2 = ymin;
  94.  
  95.                         while (y1>=ymin && x2>=xmin) {
  96.                                 Move(rp,x1,y1);
  97.                                 Draw(rp,x2,y2);
  98.                                 y1 -= deltay;
  99.                                 x2 -= deltax;
  100.                         }
  101.  
  102.                         x1 = xmax;
  103.                         y1 = ymin;
  104.                         x2 = xmin;
  105.                         y2 = ymin;
  106.  
  107.                         while (x1>=xmin && y2<=ymax) {
  108.                                 Move(rp,x1,y1);
  109.                                 Draw(rp,x2,y2);
  110.                                 x1 -= deltax;
  111.                                 y2 += deltay;
  112.                         }
  113.  
  114. /* And now the MOST important thing: RELEASE the window pointer */
  115.  
  116.                         ReleaseConsoleWindow(Output());
  117.                 } else  Printf("The output stream is no console window.\n");
  118.         } else Printf("Can't open the graphics library, V37.\n");
  119.  
  120.         return 0;
  121. }
  122.  
  123. /* This routine finds the intuition window accociated to a console
  124.    stream. It returns NULL in case the stream is no console stream.
  125.    In case the stream IS a console, this MUST be matched by a
  126.    ReleaseConsoleWindow() call or the window won't be able to get
  127.    iconfied again.
  128.  
  129.    INPUTS:      A BPTR to a filehandle
  130.    OUTPUTS:     A pointer to a intuition window, or NULL.
  131. */
  132.  
  133. struct Window *FindConsoleWindow(BPTR file)
  134. {
  135. struct FileHandle *fh;
  136. struct InfoData *info;
  137. struct Window *win;
  138. long error = ERROR_OBJECT_WRONG_TYPE;
  139.  
  140.         if (fh=BADDR(file)) {
  141.  
  142.                 /* We MUST check if fh->fh_Type points to a valid port.
  143.                    This might be NULL in case you got a handle to the
  144.                    NIL: handler */
  145.  
  146.                 if (fh->fh_Type) {
  147.  
  148.                         /* Allocate a struct InfoData. We can't take this
  149.                            from stack because it must be long word aligned */
  150.  
  151.                         if (info=AllocMem(sizeof(struct InfoData),MEMF_PUBLIC | MEMF_CLEAR)) {
  152.  
  153.                                 /* Send the packet to the handler */
  154.  
  155.                                 if (DoPkt(fh->fh_Type,ACTION_DISK_INFO,MKBADDR(info),0L,0L,0L,0L)) {
  156.  
  157.                                         /* Check whether this is a console */
  158.  
  159.                                         if (info->id_DiskType==CON_ID || info->id_DiskType==RAW_ID) {
  160.  
  161.                                                 /* Extract the window */
  162.  
  163.                                                 win = (struct Window *)(info->id_VolumeNode);
  164.                                                 error = 0;
  165.  
  166.                                                 /* More information is available here. For example,
  167.                                                    info->id_InUse
  168.                                                    points to an IORequest for communications with
  169.                                                    the console device. However, its use is dis-
  170.                                                    couraged since ViNCEd windows operate independently
  171.                                                    to the console device at all. This will point to
  172.                                                    a usable console IOStdReq, but sending out any
  173.                                                    commands with this IORequest will result in a mess.
  174.                                                    Moreover, since more than one task could request
  175.                                                    this packet at a time, you shouldn't use this
  176.                                                    struct IORequest at all.
  177.                                                    You may extract a pointer to the console device unit
  178.                                                    like this:
  179.  
  180.                                                    conunit = (struct ConUnit *)((struct IOStdReq *)id->id_InUse)->io_Unit;
  181.  
  182.                                                    However, the use of this unit is discouraged, too,
  183.                                                    since it has no meaning for ViNCEd. It tries, however,
  184.                                                    to keep the information in this unit consistent, at least
  185.                                                    partially. You MAY extract the keyboard and the TAB stop
  186.                                                    positions from there, but not much more. */
  187.  
  188.                                         }
  189.                                 }
  190.  
  191.                                 /* Now release the memory */
  192.                                 FreeMem(info,sizeof(struct InfoData));
  193.                         } else error=ERROR_NO_FREE_STORE;
  194.                 }
  195.         }
  196.  
  197. /* Set some useful error code */
  198.  
  199.         SetIoErr(error);
  200.         return win;
  201. }
  202.  
  203.  
  204.  
  205. /* This routine releases the intuition window obtained before with
  206.    FindConsoleWindow(). It MUST be called if FindConsoleWindow()
  207.    returned NON-NULL. Failure to do so will disable iconification and
  208.    the AUTO ability of the console.
  209.  
  210.    INPUTS:      A file handle
  211.  
  212. */
  213.  
  214. void ReleaseConsoleWindow(BPTR file)
  215. {
  216. struct FileHandle *fh;
  217. long error=ERROR_OBJECT_WRONG_TYPE;
  218.  
  219.         if (fh=BADDR(file)) {
  220.                 /* Check whether this is a NIL handle or not */
  221.                 if (fh->fh_Type) {
  222.                         /* If not, we simply send an ACTION_UNDISK_INFO
  223.                            to the handler. In case of the ordinary
  224.                            CON: handler, this will simply return an
  225.                            error code. However, for ViNCEd, this will work
  226.                            as it should and release the window */
  227.  
  228.                         DoPkt(fh->fh_Type,ACTION_UNDISK_INFO,0L,0L,0L,0L,0L);
  229.                         error=0;
  230.                 }
  231.         }
  232.  
  233.         SetIoErr(error);
  234. }
  235.  
  236.  
  237.  
  238.  
  239.  
  240.  
  241.